package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.model.auto.*;
import com.model.auto.resp.MajorSchoolResp;
import com.model.auto.vo.MajorSchoolInfo;
import com.service.*;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 专业解读模块
 * @author astupidcoder
 * @since 2022-09-14
 */
@RestController
@RequestMapping("/majorDetailInfo")
public class MajorDetailInfoController{
    @Autowired
    private IMajorDetailInfoService majorDetailInfoService;
    @Autowired
    private IMajorCategoryInfoService majorCategoryInfoService;
    @Autowired
    private IMajorService majorService;
    @Autowired
    private IMajorV2Service majorV2Service;
    @Autowired
    private IMajorSubjectRelService majorSubjectRelService;
    @Autowired
    private ISysDictService sysDictService;

    /**
     * 获取专业详细信息
     * @param name
     * @return
     */
    @GetMapping("getMajorDetails")
    public R getMajorDetails(String name) {
        MajorDetailInfo majorDetailInfo = majorDetailInfoService.getOne(new LambdaQueryWrapper<MajorDetailInfo>()
                .eq(name != null && !"".equals(name), MajorDetailInfo::getName, name)
                .eq(MajorDetailInfo::getEnabled, false));
        if(!ObjectUtils.isEmpty(majorDetailInfo)){
            getMajorCategoryId(majorDetailInfo);
            majorDetailInfo.setMajorScore(majorSubjectRelService.getScore(majorDetailInfo.getCode()));
        }
        return R.ok(majorDetailInfo);
    }

    private void getMajorCategoryId(MajorDetailInfo majorDetailInfo) {
        MajorCategoryInfo majorCategoryInfo = majorCategoryInfoService.getOne(new LambdaQueryWrapper<MajorCategoryInfo>()
                .eq(MajorCategoryInfo::getCode, majorDetailInfo.getCode())
                .eq(MajorCategoryInfo::getLevel, 3)
                .eq(MajorCategoryInfo::getCatType, 1)
        );
        if(!ObjectUtils.isEmpty(majorCategoryInfo)){
            majorDetailInfo.setCategoryId(majorCategoryInfo.getId());
            majorDetailInfo.setDegree(majorCategoryInfo.getGrant());
            majorDetailInfo.setEducationalSystem(majorCategoryInfo.getYears());
            MajorCategoryInfo parent = majorCategoryInfoService.getOne(new LambdaQueryWrapper<MajorCategoryInfo>()
                    .eq(MajorCategoryInfo::getId, majorCategoryInfo.getParentId()));
            if(!ObjectUtils.isEmpty(parent)){
                majorDetailInfo.setParentCategoryId(parent.getId());
                majorDetailInfo.setParentCategoryName(parent.getName());
                MajorCategoryInfo grade = majorCategoryInfoService.getOne(new LambdaQueryWrapper<MajorCategoryInfo>()
                        .eq(MajorCategoryInfo::getId, parent.getParentId()));
                if(!ObjectUtils.isEmpty(grade)){
                    majorDetailInfo.setGrandCategoryId(grade.getId());
                    majorDetailInfo.setGrandCategoryName(grade.getName());
                }
            }
        }
    }

    /**
     * 获取专业招生院校
     * @param type  1:理工 2:文史
     * @param name  专业名称
     * @return
     */
    @GetMapping("getMajorSchool")
    public R getMajorSchool(String type,String name) {
        MajorSchoolResp resp = new MajorSchoolResp();

        List<Major> list = majorService.getByTypeAndName(type, name);
        if(!ObjectUtils.isEmpty(list)){
            //获取字典当前年份
            List<SysDict> dictList = sysDictService.list(new LambdaQueryWrapper<SysDict>()
                    .eq(SysDict::getDictType, "data_year")
                    .eq(SysDict::getDictName, "now")
            );
            if(!ObjectUtils.isEmpty(dictList)){
                resp.setYear(dictList.get(0).getDictValue());
            }

            List<MajorSchoolInfo> majorSchoolInfoList = new ArrayList<>();
            int enterCount = 0;
            for(Major major:list){
                MajorSchoolInfo majorSchoolInfo = MajorSchoolInfo.toMajorSchoolInfo(major);
                majorSchoolInfoList.add(majorSchoolInfo);
                enterCount += majorSchoolInfo.getEnterCount();
            }
            resp.setCount(list.size());
            resp.setEnterCount(enterCount);
            resp.setSchoolList(majorSchoolInfoList);
        }
        return R.ok(resp);
    }

    @GetMapping("getMajorSchoolV2")
    public R getMajorSchoolV2(String type,String name) {
        MajorSchoolResp resp = new MajorSchoolResp();

        List<MajorV2> list = majorV2Service.getByTypeAndName(type, name);
        if(!ObjectUtils.isEmpty(list)){
            //获取字典当前年份
            List<SysDict> dictList = sysDictService.list(new LambdaQueryWrapper<SysDict>()
                    .eq(SysDict::getDictType, "data_year")
                    .eq(SysDict::getDictName, "now")
            );
            if(!ObjectUtils.isEmpty(dictList)){
                resp.setYear(dictList.get(0).getDictValue());
            }

            Map<String,Boolean> nameMap = new HashMap<>();
            List<MajorSchoolInfo> majorSchoolInfoList = new ArrayList<>();
            int enterCount = 0;
            for(MajorV2 major:list){
                MajorSchoolInfo majorSchoolInfo = MajorSchoolInfo.toMajorSchoolInfo(major);
                if(Objects.isNull(nameMap.get(majorSchoolInfo.getSchoolName()))){
                    majorSchoolInfoList.add(majorSchoolInfo);
                    nameMap.put(majorSchoolInfo.getSchoolName(), true);
                }
                enterCount += majorSchoolInfo.getEnterCount();
            }
            resp.setCount(list.size());
            resp.setEnterCount(enterCount);
            resp.setSchoolList(majorSchoolInfoList);
        }
        return R.ok(resp);
    }
}
